package sau.diyizu.media;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import sau.diyizu.media.music.MusicsFragment;
import sau.diyizu.media.video.VideosFragment;

import java.util.ArrayList;
import java.util.List;

/**
 @author JYD
 @version 1.0
 @since 2020/12/15 0:20 */
public class MainActivity extends AppCompatActivity {

    private static final int READ_PERMISSION_REQUEST_CODE = 1;

    private TabLayout tabLayout;
    private ViewPager content_vp;

    private ViewPagerAdapter viewPagerAdapter;

    private VideosFragment videosFragment;
    private MusicsFragment musicsFragment;

    private boolean readPermissionEnabled = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        onBindView();
        onInitView();
        onCheckPermission();
    }

    private void onBindView() {
        tabLayout = findViewById(R.id.tabLayout);
        content_vp = findViewById(R.id.content_vp);
    }

    private void onInitView() {
        tabLayout.setupWithViewPager(content_vp);
        viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        List<ViewPagerAdapter.Pager> fragments = new ArrayList<>();
        fragments.add(new ViewPagerAdapter.Pager("影音", videosFragment = new VideosFragment()));
        fragments.add(new ViewPagerAdapter.Pager("音乐", musicsFragment = new MusicsFragment()));
        viewPagerAdapter.setPagers(fragments);
        content_vp.setAdapter(viewPagerAdapter);
    }

    private void onCheckPermission() {
        if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.Q
                || ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            onReadPermissionUpdate(true);
        } else if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) {
            toast("需要文件读取权限以读取本机文件");
        } else {
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, READ_PERMISSION_REQUEST_CODE);
        }
    }

    private void onReadPermissionUpdate(boolean enabled) {
        readPermissionEnabled = enabled;
        videosFragment.onReadPermissionUpdate(enabled);
        musicsFragment.onReadPermissionUpdate(enabled);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case READ_PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0) {
                    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        toast("权限请求成功");
                        onReadPermissionUpdate(true);
                    } else {
                        toast("权限请求失败");
                        onReadPermissionUpdate(false);
                    }
                } else {
                    toast("权限请求发生未知错误");
                    onReadPermissionUpdate(false);
                }
                break;
            default:
                break;
        }

    }

    private void toast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}

